<?php
/**
 * Created by PhpStorm.
 * User: edmmss
 * Date: 2018/11/24
 * Time: 10:30
 */

namespace app\modules\dependencyInjection;


class DatabaseConnection
{
    private $_databaseConfiguration;

    /**
     * DatabaseConnection constructor.
     * @param DatabaseConfiguration $databaseConfiguration
     */
    public function __construct(DatabaseConfiguration $databaseConfiguration)
    {
        $this->_databaseConfiguration = $databaseConfiguration;
    }

    /**
     * 获取数据库dsn
     *
     * @date   2018/11/24
     * @author edmmss
     * @return string
     */
    public function getDsn()
    {
        // mysql:host=192.168.88.89
        return sprintf(
            'mysql:host=%s;port=%d;username=%s;password=%s',
            $this->getDatabaseConfiguration()->getHost(),
            $this->getDatabaseConfiguration()->getPort(),
            $this->getDatabaseConfiguration()->getUsername(),
            $this->getDatabaseConfiguration()->getPassword()
        );
    }

    /**
     * @return DatabaseConfiguration
     */
    private function getDatabaseConfiguration(): DatabaseConfiguration
    {
        return $this->_databaseConfiguration;
    }
}